/*
*这是线性表，vs2010编译通过
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define state int

typedef struct
{
	int usedLength;
	int maxLength;
	state *elem;
} Sqlist;

int ifSuccess(int );
Sqlist createNewList();
int initElement(Sqlist *);
int setElement(int ,state,Sqlist *);
int insertAElement(int ,state ,Sqlist *);
int printAllElement(Sqlist *);
int deleteElement(int ,Sqlist *);
int enlargeList(Sqlist *);
int findElement(state,Sqlist *);
state findId(int ,Sqlist *);
int deleteList(Sqlist *);
int ifListExist(Sqlist *);

int main(int argc,char *argv[])
{
	int i = 0,sti = 0,error = 1;
	while(0 <= i && i <= 9)
	{
		printf("------------\n");
		printf("线性表操作：\n");
		printf("1 创建一个新表\n");
		printf("2 销毁一个表\n");
		printf("3 向空表中添加一系列元素\n");
		printf("4 从表中删除指定位置的元素\n");
		printf("5 在表中添加一个元素\n");
		printf("6 从表中查找指定位置的元素\n");
		printf("7 查找元素在表中的位置\n");
		printf("8 检查表是否存在\n");
		printf("9 打印表中的所有元素\n");
		int id = -1;
		Sqlist list;
		if(!scanf("%d",&i))
		{
			printf("输入错误\n");
			error = 0;
			getchar();
		}else
		{
			error = 1;
		}
		if(sti)
		{
			switch(i)
			{
				case 1:
				printf("线性表已创建\n");
				break;
				case 2:
				printf("销毁一个表\n");
				ifSuccess(deleteList(&list));
				sti = 0;
				break;
				case 3:
				printf("向空表中添加一系列元素,空格隔开，ctrl^d结束\n");
				ifSuccess(initElement(&list));
				break;
				case 4:
				printf("从表中删除一个元素\n请输入元素在表中的位置");
				scanf("%d",&id);
				ifSuccess(deleteElement(id,&list));
				break;
				case 5:
				printf("在表中添加一个元素\n请输入元素在表中的位置");
				scanf("%d",&id);
				state element1;
				printf("请输入元素\n");
				scanf("%d",&element1);
				ifSuccess(insertAElement(id,element1,&list));
				break;
				case 6:
				printf("从表中查找指定位置的元素\n");
				scanf("%d",&id);
				printf("%d\n",findId(id,&list));
				break;
				case 7:
				printf("查找元素在表中的位置\n");
				state element2;
				scanf("%d",&element2);
				printf("%d\n",findElement(element2,&list));
				break;
				case 8:
				printf("检查表是否存在\n");
				if(ifListExist(&list))
				{
					printf("线性表已创建\n");
				}else
				{
					printf("线性表不存在！\n");
				}
				break;
				case 9:
				printf("打印表中的所有元素\n");
				ifSuccess(printAllElement(&list));
				break;
				default:
				printf("输入错误\n");
				break;
			}
		}else if(error)
		{
			if(i != 1) printf("表还没有创建，请先创建一个表\n");
			printf("创建一个线性表\n");
			list = createNewList();
			sti = 1;
			printf("线性表已创建\n");
		}
		system("pause");
	}
	system("pause");
	return 0;
}

int ifSuccess(int s)
{
	if(s == 0)
	{
		printf("--操作成功--\n");
	}else
	{
		return s;
	}
	return 0;
}

Sqlist createNewList()
{
	Sqlist list;
	list.usedLength = 0;
	list.maxLength = 100;
	list.elem = (state *)malloc(sizeof(state)*list.maxLength);
	return list;
}

int initElement(Sqlist *list)
{
	int start = 0;
	state element;
	while(scanf("%d",&element))
	{
		if(start == list -> maxLength)
		{
			if(enlargeList(list) != 0)
			{
				printf("扩充线性表失败，程序退出\n");
				return -1;
			}
		}
		list -> elem[start] = element;
		list -> usedLength ++;
		start ++;
	}
	getchar();
	return 0;
}

int insertAElement(int id,state element,Sqlist *list)
{
	int i;
	if(id >= 0 && id <= list -> usedLength)
	{
		if(id == list -> maxLength)
		{
			if(enlargeList(list) != 0)
			{
				printf("扩充线性表失败，程序退出\n");
				return -1;
			}
		}
		for(i = list -> usedLength;i > id;i --)
		{
			list -> elem[i] = list -> elem[i - 1];
		}
		list -> elem[id] = element;
		list -> usedLength ++;
		return 0;
	}else
	{
		return 1;
	}
}

int setElement(int id,state element,Sqlist *list)
{
	if(id >= 0 && id < list -> usedLength)
	{
		list -> elem[id] = element;
		return 0;
	}else
	{
		return 1;
	}
	return 0;
}

int printAllElement(Sqlist *list)
{
	int i;
	for(i = 0;i < list -> usedLength;i ++)
	{
		printf("%d ",list -> elem[i]);
	}
	return 0;
}

int deleteElement(int id,Sqlist *list)
{
	if(id >= 0 && id < list -> usedLength)
	{
		int i;
		for(i = id;i < list -> usedLength - 1;i ++)
		{
			list -> elem[i] = list -> elem[i + 1];
		}
		list -> usedLength --;
	}else
	{
		return 1;
	}
	return 0;
}

int enlargeList(Sqlist *list)
{
	list -> elem = (state *)realloc(list -> elem,sizeof(state)*(list -> maxLength + 100));
	if(!list -> elem)
	{
		return -1;
	}else
	{
		list -> maxLength += 100;
	}
	return 0;
}	

int findElement(state find,Sqlist *list)
{
	int i;
	for(i = 0;i < list -> usedLength;i ++)
	{
		if(list -> elem[i] == find)
		{
			return i;
		}
	}
	return -1;
}

state findId(int id,Sqlist *list)
{
	return list -> elem[id];
}

int deleteList(Sqlist *list)
{
	if(list -> maxLength != 0)
	{
		free(list -> elem);
		list -> elem = NULL;
		list -> usedLength = 0;
		list -> maxLength = 0;
		return 0;
	}else
	{
		return 1;
	}
	return 1;
}

int ifListExist(Sqlist *list)
{
	if(list -> maxLength != 0)
	{
		return 1;
	}else
	{
		return 0;
	}
	return 0;

}